<?php

declare(strict_types=1);

require_once __DIR__ . '/../vendor/autoload.php';

use SixShop\Wangdian\WangdianFactory;
use SixShop\Wangdian\Exception\ApiException;

// Create client
$client = WangdianFactory::createSandboxClient(
    sid: 'your-sid',
    appKey: 'your-app-key',
    appSecret: 'your-app-secret'
);

try {
    // Query current stock
    echo "Querying stock information...\n";
    
    $queryParams = [
        'start_time' => date('Y-m-d 00:00:00', strtotime('-7 days')),
        'end_time' => date('Y-m-d 23:59:59'),
    ];
    
    $response = $client->stock()->query($queryParams);
    
    if ($response->isSuccess()) {
        $stocks = $response->getData();
        echo "Found " . count($stocks) . " stock records\n";
        
        foreach (array_slice($stocks, 0, 10) as $stock) {
            echo "- Spec: {$stock['spec_no']}, Stock: {$stock['num']}, Warehouse: {$stock['warehouse_no']}\n";
        }
    } else {
        echo "Stock query failed: " . $response->getMessage() . "\n";
    }
    
    // Push a stockin order
    echo "\nPushing stockin order...\n";
    
    $stockinData = [
        'stockin_info' => [
            'warehouse_no' => '001',
            'stockin_type' => 1, // Manual stockin
            'outer_no' => 'STOCKIN_' . time(),
            'remark' => 'SDK测试入库',
            'details_list' => [
                [
                    'spec_no' => 'TEST001-001',
                    'num' => 50,
                    'price' => 45.0,
                    'remark' => '测试商品入库'
                ]
            ]
        ]
    ];
    
    $response = $client->stock()->pushStockinOrder($stockinData);
    
    if ($response->isSuccess()) {
        echo "Stockin order pushed successfully!\n";
        echo "Response: " . $response->toJson() . "\n";
    } else {
        echo "Stockin order push failed: " . $response->getMessage() . "\n";
    }
    
    // Push stock transfer between warehouses
    echo "\nPushing stock transfer...\n";
    
    $transferData = [
        'transfer_info' => [
            'from_warehouse_no' => '001',
            'to_warehouse_no' => '002',
            'outer_no' => 'TRANSFER_' . time(),
            'remark' => 'SDK测试调拨',
            'details_list' => [
                [
                    'spec_no' => 'TEST001-001',
                    'num' => 10,
                    'remark' => '仓库间调拨'
                ]
            ]
        ]
    ];
    
    $response = $client->stock()->pushTransfer($transferData);
    
    if ($response->isSuccess()) {
        echo "Stock transfer pushed successfully!\n";
        echo "Response: " . $response->toJson() . "\n";
    } else {
        echo "Stock transfer push failed: " . $response->getMessage() . "\n";
    }
    
    // Query stock transfers
    echo "\nQuerying stock transfers...\n";
    
    $response = $client->stock()->queryTransfer([
        'start_time' => date('Y-m-d 00:00:00'),
        'end_time' => date('Y-m-d 23:59:59'),
    ]);
    
    if ($response->isSuccess()) {
        $transfers = $response->getData();
        echo "Found " . count($transfers) . " transfers today\n";
        
        foreach (array_slice($transfers, 0, 5) as $transfer) {
            echo "- Transfer No: {$transfer['transfer_no']}, Status: {$transfer['status']}\n";
        }
    } else {
        echo "Transfer query failed: " . $response->getMessage() . "\n";
    }

} catch (ApiException $e) {
    echo "API Error: " . $e->getMessage() . "\n";
    echo "API Code: " . $e->getApiCode() . "\n";
} catch (Exception $e) {
    echo "Error: " . $e->getMessage() . "\n";
}